/**
 * 邮件配置相关的API服务
 */

import { request } from '@umijs/max';

// 加密类型枚举
export enum EncryptionType {
  NONE = 'none',
  SSL = 'ssl',
  STARTTLS = 'starttls',
}

// 邮件配置接口定义
export interface EmailConfigCreate {
  name: string;
  smtp_host: string;
  smtp_port: number;
  username: string;
  password: string;
  from_email: string;
  from_name?: string;
  encryption: EncryptionType;
  timeout: number;
  description?: string;
}

export interface EmailConfigUpdate {
  name?: string;
  smtp_host?: string;
  smtp_port?: number;
  username?: string;
  password?: string;
  from_email?: string;
  from_name?: string;
  encryption?: EncryptionType;
  timeout?: number;
  description?: string;
}

export interface EmailConfigItem {
  id: number;
  name: string;
  smtp_host: string;
  smtp_port: number;
  username: string;
  from_email: string;
  from_name?: string;
  encryption: EncryptionType;
  timeout: number;
  description?: string;
  user_id: number;
  is_active: boolean;
  created_at: string;
  updated_at: string;
}

export interface EmailConfigList {
  items: EmailConfigItem[];
  total: number;
  page: number;
  size: number;
  pages: number;
}

export interface EmailConfigTest {
  smtp_host: string;
  smtp_port: number;
  username: string;
  password: string;
  from_email: string;
  encryption: EncryptionType;
  timeout: number;
  test_email?: string;
}

export interface EmailConfigTestResponse {
  success: boolean;
  message: string;
  details?: any;
  test_email_sent: boolean;
}

export interface EmailSendTest {
  config_id: number;
  to_email: string;
  subject: string;
  content: string;
}

export interface EmailSendTestResponse {
  success: boolean;
  message: string;
  message_id?: string;
  details?: any;
}

export interface EmailConfigStatistics {
  total: number;
  active: number;
  by_encryption: Record<string, number>;
  recent_added: number;
}

// API服务函数
export async function getEmailConfigs(params: {
  page?: number;
  size?: number;
  search?: string;
  active_only?: boolean;
}): Promise<EmailConfigList> {
  return request('/api/email-configs/', {
    method: 'GET',
    params,
  });
}

export async function getEmailConfig(id: number): Promise<EmailConfigItem> {
  return request(`/api/email-configs/${id}`, {
    method: 'GET',
  });
}

export async function createEmailConfig(data: EmailConfigCreate): Promise<EmailConfigItem> {
  return request('/api/email-configs/', {
    method: 'POST',
    data,
  });
}

export async function updateEmailConfig(id: number, data: EmailConfigUpdate): Promise<EmailConfigItem> {
  return request(`/api/email-configs/${id}`, {
    method: 'PUT',
    data,
  });
}

export async function deleteEmailConfig(id: number): Promise<{ message: string }> {
  return request(`/api/email-configs/${id}`, {
    method: 'DELETE',
  });
}

export async function toggleEmailConfigStatus(id: number): Promise<EmailConfigItem> {
  return request(`/api/email-configs/${id}/toggle`, {
    method: 'POST',
  });
}

export async function testEmailConfig(data: EmailConfigTest): Promise<EmailConfigTestResponse> {
  return request('/api/email-configs/test', {
    method: 'POST',
    data,
  });
}

export async function sendTestEmail(data: EmailSendTest): Promise<EmailSendTestResponse> {
  return request('/api/email-configs/send-test', {
    method: 'POST',
    data,
  });
}

export async function getEmailConfigStatistics(): Promise<EmailConfigStatistics> {
  return request('/api/email-configs/statistics', {
    method: 'GET',
  });
}

export async function getActiveEmailConfigs(): Promise<EmailConfigItem[]> {
  return request('/api/email-configs/active/list', {
    method: 'GET',
  });
}
